﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities.Dispatcher;
using Common.Utilities;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class PdfDocListInstruction:InstructionBase
    {
        /// <summary>
        /// method to retrieve doc when missing
        /// </summary>
        public enum DocListCompensationMethod
        {
            /// <summary>
            /// no compensation
            /// </summary>
            None = 0,
            /// <summary>
            /// based on db list only
            /// </summary>
            DbToLocalFile = 1,
            /// <summary>
            /// based on local file list only
            /// </summary>
            LocalFileToDb = 2,
            /// <summary>
            /// always compensate, union of db list and local file list
            /// </summary>
            DbLocalFileTwoWayCompensate = 3
        }

        #region props
        private string _ConnStr;
        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._ConnStr;
            }
            set
            {
                this._ConnStr = value;
            }
        }

        private string _SelectSqlCommand;
        /// <summary></summary>
        public string SelectSqlCommand
        {
            get
            {
                return this._SelectSqlCommand;
            }
            set
            {
                this._SelectSqlCommand = value;
            }
        }

        private string _DocIDField;
        /// <summary></summary>
        public string DocIDField
        {
            get
            {
                return this._DocIDField;
            }
            set
            {
                this._DocIDField = value;
            }
        }

        private string _BFileFieldName;
        /// <summary></summary>
        public string BFileFieldName
        {
            get
            {
                return this._BFileFieldName;
            }
            set
            {
                this._BFileFieldName = value;
            }
        }

        private string _FileNameField;
        /// <summary></summary>
        public string FileNameField
        {
            get
            {
                return this._FileNameField;
            }
            set
            {
                this._FileNameField = value;
            }
        }

        private string _FileNameFormat;
        /// <summary>
        /// default to {0}, 
        /// if {0}.pdf, it will add a .pdf extention 
        /// to the end as file name
        /// </summary>
        public string FileNameFormat
        {
            get
            {
                return this._FileNameFormat;
            }
            set
            {
                this._FileNameFormat = value;
            }
        }

        private string _LocalFolderPath;
        /// <summary></summary>
        public string LocalFolderPath
        {
            get
            {
                return this._LocalFolderPath;
            }
            set
            {
                this._LocalFolderPath = value;
            }
        }

        private string _FileFilter;
        /// <summary></summary>
        public string FileFilter
        {
            get
            {
                return this._FileFilter;
            }
            set
            {
                this._FileFilter = value;
            }
        }

        private bool _IncludeSubfolders;
        /// <summary></summary>
        public bool IncludeSubfolders
        {
            get
            {
                return this._IncludeSubfolders;
            }
            set
            {
                this._IncludeSubfolders = value;
            }
        }

        private string _ResultFilePath;
        /// <summary></summary>
        public string ResultFilePath
        {
            get
            {
                return this._ResultFilePath;
            }
            set
            {
                this._ResultFilePath = value;
            }
        }

        private DocListCompensationMethod _Compensation;
        /// <summary></summary>
        public DocListCompensationMethod Compensation
        {
            get
            {
                return this._Compensation;
            }
            set
            {
                this._Compensation = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public PdfDocListInstruction():base(Guid.Empty, Guid.Empty)
        {
            this._ConnStr = string.Empty;
            this._SelectSqlCommand = string.Empty;
            this._DocIDField = string.Empty;
            this._BFileFieldName = string.Empty;
            this._FileNameField = string.Empty;
            this._FileNameFormat = "{0}";
            this._LocalFolderPath = string.Empty;
            this._FileFilter = "*.pdf";
            this._IncludeSubfolders = true;
            this._ResultFilePath = string.Empty;
            this._Compensation = DocListCompensationMethod.DbToLocalFile;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="connStr"></param>
        /// <param name="sql"></param>
        /// <param name="docIDField"></param>
        /// <param name="bfileField"></param>
        /// <param name="fileNameField"></param>
        /// <param name="fileNameFormat"></param>
        /// <param name="localFolderPath"></param>
        /// <param name="fileFilter"></param>
        /// <param name="includeSubfolder"></param>
        /// <param name="resultFilePath"></param>
        /// <param name="compensateMethod"></param>
        public PdfDocListInstruction(Guid componentID, Guid jobTicketID,
            string connStr, 
            string sql,
            string docIDField,
            string bfileField,
            string fileNameField,
            string fileNameFormat, 
            string localFolderPath,
            string fileFilter,
            bool includeSubfolder,
            string resultFilePath,
            DocListCompensationMethod compensateMethod)
            : base(componentID, jobTicketID)
        {
            this._ConnStr = connStr;
            this._SelectSqlCommand = sql;
            this._DocIDField = docIDField;
            this._BFileFieldName = bfileField;
            this._FileNameField = fileNameField;
            this._FileNameFormat = fileNameFormat;
            this._LocalFolderPath = localFolderPath;
            this._FileFilter = fileFilter;
            this._IncludeSubfolders = includeSubfolder;
            this._ResultFilePath = resultFilePath;
            this._Compensation = compensateMethod;
        }
        #endregion

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnectionString", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SelectSqlStatement", this.SelectSqlCommand);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DocIDField", this.DocIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "BFileField", this.BFileFieldName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileNameField", this.FileNameField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileNameFormat", this.FileNameFormat);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LocalFolderPath", this.LocalFolderPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileFilter", this.FileFilter);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IncludeSubfolder", this.IncludeSubfolders.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ResultFilePath", this.ResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CompensateMethod",((int)this.Compensation).ToString());
            
            return xNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                PdfDocListInstruction instruction = new PdfDocListInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnectionString", "");
                instruction.SelectSqlCommand = XmlDataUtil.GetAttributeValue(xNode, "SelectSqlStatement", "");
                instruction.DocIDField = XmlDataUtil.GetAttributeValue(xNode, "DocIDField", "");
                instruction.BFileFieldName = XmlDataUtil.GetAttributeValue(xNode, "BFileField", "");
                instruction.FileNameField = XmlDataUtil.GetAttributeValue(xNode, "FileNameField", "");
                instruction.FileNameFormat = XmlDataUtil.GetAttributeValue(xNode, "FileNameFormat", "{0}");
                instruction.LocalFolderPath = XmlDataUtil.GetAttributeValue(xNode, "LocalFolderPath", "");
                instruction.FileFilter = XmlDataUtil.GetAttributeValue(xNode, "FileFilter", "*.pdf");
                instruction.IncludeSubfolders = bool.Parse(XmlDataUtil.GetAttributeValue(xNode, "IncludeSubfolder", false.ToString()));
                instruction.ResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "ResultFilePath", "");
                instruction.Compensation =
                    (DocListCompensationMethod)
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "CompensateMethod",
                                                            ((int) DocListCompensationMethod.DbToLocalFile).ToString()));
                
                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }
    }
}
